package com.gz.seckill.servlet;

import com.gz.seckill.redis.SecKillRedis;
import com.gz.seckill.redis.SecKillRedisByLua;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

@WebServlet(urlPatterns = "/secKillServlet")
public class SecKillServlet extends HttpServlet {
    //这里用javaWeb实现，那么用springboot更加容易实现

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.请求时模拟生成一个userId
        String userId=new Random().nextInt(10000)+"";
        //2.获取用户要购买的票的编号
        String ticketNo = request.getParameter("ticketNo");

        //3.调用秒杀方法
//        Boolean isOk=SecKillRedis.doSecKillRedis(userId,ticketNo);
        Boolean isOk= SecKillRedisByLua.doSecKill(userId,ticketNo);

        //4.将结果返回给前端
        response.getWriter().print(isOk);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
